<template>
  <div class="chat">
    <img class="avator" v-if="unit.chatType == 1" :src="unit.fromAvatar" alt="" />
    <img class="avator" v-else src="@/assets/avatar.jpg" alt="" />
    <div style="flex: 1;">
      <div class="name-content">
        <div class="name" v-if="unit.chatType == 1">{{ unit.fromName }}</div>
        <div class="name" v-else>{{ unit.roomName }}</div>
        <div class="time">{{ unit.sendTime }}</div>
      </div>
      <div class="chat-con">
        <template v-if="unit.msgType === 'text'">
          <span class="text">
            {{ content }}
          </span>
        </template>
        <template v-else-if="'image,emotion'.includes(unit.msgType)">
          <img style="width: 100%; border-radius: 8px;" :src="content" fit="fit" />
        </template>
        <template v-else-if="unit.msgType === 'revoke'">已撤回</template>
        <template v-else-if="unit.msgType === 'voice'">
          <img
            style="width: 16px;"
            src="https://link-wechat-1251309172.cos.ap-nanjing.myqcloud.com/2023-05-23/t1684824840500-jL6kqfffCMrvSOhh.png"
            alt=""
          />
          <span style="font-size: 12px; color: #999;">语音消息</span>
        </template>
        <template v-else-if="unit.msgType === 'meeting_voice_call'">
          <img
            style="width: 16px;"
            src="https://link-wechat-1251309172.cos.ap-nanjing.myqcloud.com/2023-05-23/t1684826884331-rBz3uuCrYSGSvUY8.png"
            alt=""
          />
          <span style="font-size: 12px; color: #999;">语音通话</span>
        </template>
        <template v-else-if="unit.msgType === 'video'">
          <video
            id="video"
            class="video-js vjs-default-skin vjs-big-play-centered"
            controls
            webkit-playsinline="true"
            playsinline="true"
            :autoplay="false"
            preload="none"
            style="height: 100%; width: 100%;"
          >
            <source :src="content" type="video/mp4" />
          </video>
        </template>
        <a v-else-if="'file'.includes(unit.msgType)" class="msgtypefile">
          {{ content }}
        </a>
        <a
          v-else-if="'docmsg'.includes(unit.msgType)"
          class="msgtypefile"
          :href="JSON.parse(unit.contact).link_url"
          target="_black"
        >
          {{ content }}
        </a>
        <div v-else-if="unit.msgType === 'weapp'">
          <div class="card-content">{{ content }}</div>
          <div class="card-foot">小程序</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'show-chat',
    data() {
      return {}
    },
    props: {
      unit: {
        type: Object,
        default: {}
      }
    },
    computed: {
      content() {
        let typeDict = {
          text: 'content',
          image: 'attachment',
          emotion: 'attachment',
          file: 'filename',
          docmsg: 'title',
          video: 'attachment',
          voice: 'attachment',
          location: 'address',
          weapp: 'title'
        }
        let attr = typeDict[this.unit.msgType]
        if (attr) {
          return JSON.parse(this.unit.contact)[typeDict[this.unit.msgType]]
        } else {
          return JSON.parse(this.unit.contact)
        }
      }
    },
    methods: {}
  }
</script>

<style lang="less" scoped>
  .msgtypefile {
    margin: 5px;
    width: 200px;
    display: inline-block;
    line-height: 30px;
    cursor: pointer;
  }
  .chat {
    padding: 16px;
    background: #f2f3f5;
    border-radius: 16px;
    display: flex;
    align-items: flex-start;
    .avator {
      height: 48px;
      width: 48px;
      flex-shrink: 0;
      margin-right: 8px;
      border-radius: 8px;
    }
    .name-content {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .name {
        flex: 1;
        width: 0;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        font-size: 14px;
        font-weight: 500;
        color: #1d2129;
      }
      .time {
        flex-shrink: 0;
        font-size: 14px;
        font-weight: 500;
        color: #86909c;
      }
    }
    .chat-con {
      margin-top: 8px;
      background: #ffffff;
      border-radius: 0px 12px 12px 12px;
      padding: 8px 16px;
      .text {
        line-height: 24px;
        font-size: 15px;
        font-weight: 500;
        color: #4e5969;
      }
    }
  }
</style>
